<?php
include '../../auth/ceklogin.php';
include '../../sql/conn.php';
if(!isset($_REQUEST['temp'])){
    $idQuotation=$_REQUEST['idQuotation'];
    
    //select data temporary
    $query_update1=mysql_query("select * from quotation_item where idQuotation='$idQuotation' and priceItem like 'temp%'");
    if($query_update1){
        while($row_update1=mysql_fetch_object($query_update1)){
            $test=$row_update1->priceItem;
            if($test[0]=='t'){
                $temp=explode('temp',$test);
                $row_update1->priceItem=$temp[1];
            }
            $query_update2=mysql_query("
                    UPDATE quotation_item 
                    SET priceItem='$row_update1->priceItem' 
                    WHERE idQuotation_item='$row_update1->idQuotation_item'
                    ");
            if($query_update2){
                $status="sukses";
            } 
            else {
                $status="gagal";
            }
        }
        
        $now=date("Y-m-d H:i:s");
        $user=$_SESSION['userbkknt'];
        $query=mysql_query("
        UPDATE
                `bukakainti`.`quotation` 
        SET  
                `chgdateQuotation` =  now(),
                `chgbyQuotation` =  '$user'
        WHERE 
                CONCAT(  `quotation`.`idQuotation` ) ='$idQuotation' 
        LIMIT 1 ;
        ");
        if($query){
            echo json_encode(array('success'=>true,'msg'=>$no));
        } 
        else {
            echo json_encode(array('msg'=>'Error Occured.'.  mysql_error()));
        }

    } else {
        echo json_encode(array('msg'=>'Error Occured.'.  mysql_error()));
    }

}
else {
    //ngambil data parsingan
    $idQuotation_item=$_REQUEST['id'];
    $idBarang=$_REQUEST['idBarang'];
    $qtyBarang=$_REQUEST['qtyBarang'];
    $priceItem=$_REQUEST['priceItem'];
    
    if($_REQUEST['temp']=='x'){
        $query_update_quotation_item_temp=mysql_query("
            UPDATE
                `bukakainti`.`quotation_item`
            SET
                `idBarang` = '$idBarang',
                `qtyBarang` = '$qtyBarang',
                `priceItem` = '$priceItem' 
            WHERE
                CONCAT ( `quotation_item`.`idQuotation_item` ) = '$idQuotation_item'
            LIMIT 1
            ");
    }
    elseif($_REQUEST['temp']=='editx'){
        $idQuotation=$_REQUEST['idQuotation'];
        $query_update_quotation_item_temp=mysql_query("
            UPDATE
                `bukakainti`.`quotation_item`
            SET
                `idBarang` = '$idBarang',
                `qtyBarang` = '$qtyBarang',
                `priceItem` = 'temp$priceItem' 
            WHERE
                CONCAT ( `quotation_item`.`idQuotation_item` ) = '$idQuotation_item'
            LIMIT 1
            ");
    }
    
    if($query_update_quotation_item_temp){
        echo json_encode(array('success'=>true));
    }
    else{
        echo json_encode(array('msg'=>  mysql_error()));
    }
    
}
?>